package com.sofwin.yyb.controller;

import com.sofwin.yyb.entity.Mechanism;
import com.sofwin.yyb.entity.Course;
import com.sofwin.yyb.entity.Appointment;
import com.sofwin.yyb.entity.User;
import com.sofwin.yyb.service.MechanismService;
import com.sofwin.yyb.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/merchant")
public class MechanismController {
    @Autowired
    AppointmentService appointmentService;
    @Autowired
    MechanismService mechanismService;

    /**
     * 客户申请成为商户
     * @param mechanism
     * @param session
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> addMerchant(
            Mechanism mechanism,
            HttpSession session,
            @RequestParam(value = "file",required = false)MultipartFile file){
        User user = (User)session.getAttribute("user");
        String license = "../img/"+file.getOriginalFilename();
        mechanism.setLicense(license);
        mechanism.setMId(user.getId());
        mechanismService.addMerchant(mechanism);
        Map<String,Object> map = new HashMap<>();
        return map;
    }

    /**
     * 判断用户是否已申请为商户
     * @param session
     * @return
     */
    @RequestMapping("/cusOrMer")
    @ResponseBody
    public Mechanism findCusOrMer(HttpSession session){
        User user = (User)session.getAttribute("user");
        Mechanism mechanism = mechanismService.findByUserId(user.getId());
        return mechanism;
    }

    /**
     * 重定向到商户订单页面
     * @return
     */
    @RequestMapping("/toMoList")
    public String toMoList(Integer status,HttpSession session){
        session.setAttribute("status",status);
        return "redirect:/app/mo_list.html";
    }
    @RequestMapping("/orderList")
    @ResponseBody
    public List<Appointment> orderList(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        User user = (User)session.getAttribute("user");
        Integer status = (Integer)session.getAttribute("status");
        map.put("cId",user.getId());
        map.put("status",status);
        List<Appointment> list = appointmentService.findMListByStatus(map);
        return list;
    }
    /**
     * 修改机构订单状态
     */
    @RequestMapping("/received")
    @ResponseBody
     public Map<String,Object> received(Integer id,Integer status){
        Map<String,Object> map = new HashMap<>();
        map.put("aId",id);
        map.put("status",status);
        appointmentService.updateMStatus(map);
        return map;
     }

    /**
     * 获取商家所有服务
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/serviceList")
     public List<Course> findServiceList(HttpSession session){
        User user = (User)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user.getId());
        List<Course> list = mechanismService.findServiceList(map);
        return list;
     }
}
